﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommonTestingUtils;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace LightPersist.DomainGenerator.Tests
{
	[TestClass]
	public class Usability : FixtureMsTest
	{
		[TestMethod]
		public void Should_not_allow_create_domain_without_name()
		{
			var domBad = @"<DomainMetainfo xmlns='LightPersist' />";
			var domGood = @"<DomainMetainfo xmlns='LightPersist' DomainName='test' />";

			var gen = new LightPersistModelGenerator();
			
			var ex = Throws(()=> gen.GenerateCode(domBad, null));

			Assert.IsTrue(ex.Message.Contains("DomainMetainfo.DomainName"));
			Assert.IsTrue(ex.Message.Contains("not defined"));
			
			NotThrows(()=> gen.GenerateCode(domGood, null));
		}

		[TestMethod]
		public void Should_not_allow_type_without_name()
		{
			var domBad = @"<DomainMetainfo xmlns='LightPersist' DomainName='test'><ClassMetainfo /></DomainMetainfo>";
			var domGood = @"<DomainMetainfo xmlns='LightPersist' DomainName='test'><ClassMetainfo Name='a' /></DomainMetainfo>";

			var gen = new LightPersistModelGenerator();

			var ex = Throws(() => gen.GenerateCode(domBad, null));

			Assert.IsTrue(ex.Message.Contains("ClassMetainfo.Name"), ex.Message);
			Assert.IsTrue(ex.Message.Contains("not defined"), ex.Message);

			NotThrows(() => gen.GenerateCode(domGood, null));
		}

		[TestMethod]
		public void Should_not_allow_property_without_type()
		{
			var domBad = @"<DomainMetainfo xmlns='LightPersist' DomainName='test'><ClassMetainfo Name='MyClassName'>
<PropertyMetainfo Name='MyPropertyName' />
</ClassMetainfo></DomainMetainfo>";
			var domGood = @"<DomainMetainfo xmlns='LightPersist' DomainName='test'><ClassMetainfo Name='MyClassName'>
<PropertyMetainfo Name='MyPropertyName' Type='int32' />
</ClassMetainfo></DomainMetainfo>";

			var gen = new LightPersistModelGenerator();

			var ex = Throws(() => gen.GenerateCode(domBad, null));

			Assert.IsTrue(ex.Message.Contains("MyClassName"), ex.Message);
			Assert.IsTrue(ex.Message.Contains("MyPropertyName"), ex.Message);
			Assert.IsTrue(ex.Message.Contains("PropertyMetainfo.Type"), ex.Message);
			Assert.IsTrue(ex.Message.Contains("not defined"), ex.Message);

			NotThrows(() => gen.GenerateCode(domGood, null));
		}

		[TestMethod]
		public void Should_not_allow_property_without_name()
		{
			var domBad = @"<DomainMetainfo xmlns='LightPersist' DomainName='test'><ClassMetainfo Name='MyClassName'>
<PropertyMetainfo Type='int32' />
</ClassMetainfo></DomainMetainfo>";
			var domGood = @"<DomainMetainfo xmlns='LightPersist' DomainName='test'><ClassMetainfo Name='MyClassName'>
<PropertyMetainfo Name='MyPropertyName' Type='int32' />
</ClassMetainfo></DomainMetainfo>";

			var gen = new LightPersistModelGenerator();

			var ex = Throws(() => gen.GenerateCode(domBad, null));

			Assert.IsTrue(ex.Message.Contains("MyClassName"), ex.Message);
			Assert.IsTrue(ex.Message.Contains("PropertyMetainfo.Name"), ex.Message);
			Assert.IsTrue(ex.Message.Contains("not defined"), ex.Message);

			NotThrows(() => gen.GenerateCode(domGood, null)); 

		}
	}
}
